package com.sanhe.a448.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.menu.app.MyApp;
import com.menu.app.activity.base.MenuBaseActivity;
import com.menu.app.util.Utils;
import com.plata.base.aclr.utils.SPHelper;
import com.sanhe.a448.databinding.ActivityInfoBinding;
import com.sanhe.a448.event.DayEvent;
import com.sanhe.a448.event.Event;
import com.sanhe.a448.event.HourEvent;
import com.sanhe.a448.event.MinEvent;
import com.sanhe.a448.event.VersionEvent;
import com.sanhe.a448.utils.Const;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class InfoActivity extends MenuBaseActivity {

    ActivityInfoBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityInfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Const.sendTouchEvent(0x83, 0x10, 0xc, 1, 0x1c);

        binding.back.setOnClickListener(v -> finish());
        binding.msn.setOnClickListener(v -> {
            showPwd();
        });

        updateTime();
        onMessageEvent(Event.version);
        initBall(binding.frame);
    }

    AlertDialog dlg = null;

    private void showPwd() {
        EditText et = new EditText(this);
        et.setHint(com.menu.app.R.string.pwd);
        et.setMaxLines(1);
        et.setTextSize(40);
        et.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        et.setImeOptions(EditorInfo.IME_ACTION_DONE);
        et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && actionId == EditorInfo.IME_ACTION_UNSPECIFIED && event.getAction() == KeyEvent.ACTION_DOWN) || actionId == EditorInfo.IME_ACTION_DONE
                        || (event != null
                        && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event
                        .getAction() == KeyEvent.ACTION_DOWN)) {
                    Utils.hideInput(et);
                    String s = et.getText().toString().trim();
                    if (s.equals("158158")) {
                        Intent i = new Intent(InfoActivity.this, SetActivity.class);
                        startActivity(i);
                    }
                    return true;
                }
                return false;
            }
        });
        dlg = new AlertDialog.Builder(this).setView(et)
                .setPositiveButton(com.menu.app.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Utils.hideInput(et);
                        String s = et.getText().toString().trim();
                        if (s.equals("158158")) {
                            Intent i = new Intent(InfoActivity.this, SetActivity.class);
                            startActivity(i);
                        }
                    }
                }).setNegativeButton(com.menu.app.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Utils.hideInput(et);
                    }
                }).create();
        dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                dlg = null;
            }
        });
        dlg.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.msn.setText(Utils.getMsn());
        long time = SPHelper.getInstance(MyApp.getApp()).getLong("end_time", 0);
        if (time > 0) {
            binding.rentLayout.setVisibility(View.VISIBLE);
            binding.rent.setText(Utils.getDay4(time));
        } else {
            binding.rentLayout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (dlg != null) {
            dlg.dismiss();
        }
    }

    @Override
    protected byte[][] getInterest() {
        return new byte[0][];
    }

    @Override
    protected void updateUI() {

    }

    @Override
    public void exit() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(DayEvent event) {
        updateTime();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(HourEvent event) {
        updateTime();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MinEvent event) {
        updateTime();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(VersionEvent event) {
        binding.ver.setText(event.toString());
    }

    private void updateTime() {
//        String str = String.format("%sd %sh %sm", Event.day.toString(), Event.hour.toString(), Event.min.toString());
//        binding.time.setText(str);
    }

    @Override
    public String getPageIndex() {
        return "";
    }
}
